<?php

namespace App\Models;

use App\Http\Helper\OSS;

/**
 * Class Picture
 * @property string original_url 图片链接
 * @property integer height 高度
 * @property integer width 宽度
 * @property integer type_id 类型ID
 * @property string type 类型（model_name）
 */
class Picture extends ApplicationModel
{
    protected $fillable = [
        'original_url',
        'height',
        'width',
        'type_id',
        'type',
    ];

    public static function upload_user_avatar($pic)
    {
        return self::generate_pic_path('uploads/assets/user/avatars/' . date('ym') . '/', $pic);
    }

    public static function upload_product_pic($pic)
    {
        return self::generate_pic_path('uploads/assets/product/pictures/' . date('ym') . '/', $pic);
    }

    public static function upload_newproduct_pic($pic)
    {
       // return self::generate_newpic_path('uploads/assets/product/pictures/', $pic);
        return self::generate_pic_path('uploads/assets/product/pictures/2017-07-11/', $pic);
    }

    public static function upload_skill_pic($pic)
    {
        return self::generate_pic_path('uploads/assets/skills/', $pic);
    }

    public static function upload_topic_pic($pic)
    {
        return self::generate_pic_path('uploads/assets/topics/', $pic);
    }
    public static function upload_theme_pic($pic,$ext)
    {
        return self::generate_pic_path_with_ext('uploads/assets/themes/', $pic,$ext);
    }
    public static function upload_banner_pic($pic)
    {
        return self::generate_pic_path('uploads/assets/banners/', $pic);
    }

    public static function upload_interest_pic($pic)
    {
        return self::generate_pic_path('uploads/assets/interests/', $pic);
    }

    public static function upload_dynamic_pic($pic)
    {
        return self::generate_pic_path('uploads/assets/dynamic/', $pic);
    }

    public static function upload_declaimer_pic($pic)
    {
        return self::generate_pic_path('uploads/assets/declaimer/', $pic);
    }

    public static function generate_pic_path($desc, $pic)
    {
//        list($filename, $ext) = explode(".", $pic->getClientOriginalName());
        $object = $desc . date('Ymd') . '/' . md5($pic);
        if (self::upload_oss($object, $pic)) {
            return self::static_img_host() . $object;
        }
        return false;
    }

    public static function generate_pic_path_with_ext($desc, $pic,$ext='')
    {
//        list($filename, $ext) = explode(".", $pic->getClientOriginalName());
        $object = $desc . date('Ymd') . '/' . md5($pic).$ext;
        if (self::upload_oss($object, $pic)) {
            return self::static_img_host() . $object;
        }
        return false;
    }

    public static function generate_newpic_path($desc, $pic)
    {
//        list($filename, $ext) = explode(".", $pic->getClientOriginalName());
        $object = $desc . date('Ymd') . '/' .$pic;
        if (self::upload_oss($object, $pic)) {
            return self::static_img_host() . $object;
        }
        return false;
    }
    public static function get_pic_size($pic_url)
    {

    }

    public function original_url()
    {
        $origin = starts_with($this->original_url, 'http') ? $this->original_url : $this->img_host() . $this->original_url;
        $origin = $origin.'?x-oss-process=image/format,webp';
        return $origin;
    }

    public static function upload_oss($desc, $pic)
    {
        $isok = OSS::upload($desc, $pic);
        if ($isok) {
            return true;
        }
        return false;
    }
}
